package com.aftersale.platform.entity;

import lombok.Data;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class Ticket {
    private Integer id;
    private Integer userId; // 关联到用户ID
    private String username; // 用于前端展示，不存储在数据库，通过JOIN或Service层设置
    private String ticketType;
    private String description;
    private TicketStatus status;
    private LocalDate submitDate;
    private String progress; // 存储JSON字符串或逗号分隔的字符串
    private LocalDateTime createdAt;
    private LocalDateTime updatedAt;

    public enum TicketStatus {
        PENDING,     // 待处理
        PROCESSING,  // 处理中
        COMPLETED,   // 已完成
        CLOSED       // 已关闭
    }

    // 辅助方法：将progress字符串转换为List<String>
    public List<String> getProgressList() {
        if (this.progress == null || this.progress.isEmpty()) {
            return List.of();
        }
        // 假设progress是JSON数组字符串，这里进行简化处理为逗号分隔的字符串
        // 实际生产中推荐使用Jackson等库进行JSON序列化和反序列化
        String cleanedProgress = this.progress.replace("[", "").replace("]", "").replace("\"", "");
        return Arrays.stream(cleanedProgress.split(","))
                .map(String::trim)
                .filter(s -> !s.isEmpty())
                .collect(Collectors.toList());
    }

    // 辅助方法：将List<String>转换为progress字符串
    public void setProgressList(List<String> progressList) {
        if (progressList == null || progressList.isEmpty()) {
            this.progress = null;
        } else {
            // 简化处理为逗号分隔的字符串，实际生产中推荐使用Jackson等库进行JSON序列化
            this.progress = "[\"" + String.join("\",\"", progressList) + "\"]";
        }
    }
}